|
1
|
|
|
var SDK = require('@ama-team/voxengine-sdk') |
|
2
|
|
|
var Slf4j = SDK.Logger.Slf4j |
|
3
|
|
|
var Future = SDK.Concurrent.Future |
|
4
|
|
|
var Errors = require('../../Error') |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* @typedef {object} Branch~Options |
|
8
|
|
|
* |
|
9
|
|
|
* @property {String} name |
|
10
|
|
|
* @property {TStateHandler} handler |
|
11
|
|
|
* @property {LoggerOptions} logger |
|
12
|
|
|
* @property {IExecutor} executor |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Represents single transition execution branch (main / abort). It's |
|
17
|
|
|
* entrypoint, #run() method, triggers it's execution and returns a promise |
|
18
|
|
|
* that either resolves with whatever handler returns or rejects with error. |
|
19
|
|
|
* |
|
20
|
|
|
* @param {Branch~Options} options |
|
21
|
|
|
* @class |
|
22
|
|
|
*/ |
|
23
|
|
|
function Branch (options) { |
|
24
|
|
|
var result = new Future() |
|
25
|
|
|
var triggered = false |
|
26
|
|
|
var loggerName = 'ama-team.vsf.execution.transition.branch' |
|
27
|
|
|
var logger = Slf4j.factory(options.logger, loggerName) |
|
28
|
|
|
logger.attach('name', options.name) |
|
29
|
|
|
|
|
30
|
|
|
function run (origin, hints, token) { |
|
31
|
|
|
if (triggered) { |
|
32
|
|
|
var message = 'Tried to run execution branch ' + options.name + ' twice' |
|
33
|
|
|
throw new Errors.IllegalStateError(message) |
|
34
|
|
|
} |
|
35
|
|
|
triggered = true |
|
36
|
|
|
logger.debug('Launched') |
|
37
|
|
|
options.executor.runHandler(options.handler, [origin, hints], token) |
|
38
|
|
|
.then(function (value) { |
|
39
|
|
|
logger.debug('Execution branch has finished with {}', value) |
|
40
|
|
|
result.resolve(value) |
|
41
|
|
|
}, function (reason) { |
|
42
|
|
|
logger.debug('Execution branch has failed with reason {}', reason) |
|
43
|
|
|
result.reject(reason) |
|
44
|
|
|
}) |
|
45
|
|
|
return result |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Runs execution branch. |
|
50
|
|
|
* |
|
51
|
|
|
* @param {TState} origin |
|
52
|
|
|
* @param {THints} hints |
|
53
|
|
|
* @param {CancellationToken} [token] |
|
54
|
|
|
* |
|
55
|
|
|
* @return {Thenable} |
|
56
|
|
|
*/ |
|
57
|
|
|
this.run = run |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
module.exports = { |
|
61
|
|
|
Branch: Branch |
|
62
|
|
|
} |
|
63
|
|
|
|